package com.hs.ph.Servlet;

import com.alibaba.fastjson.JSON;
import com.hs.ph.Dao.Impl.spglDaoimpl;
import com.hs.ph.Dao.SpglDao;
import com.hs.ph.Entity.Result;
import com.hs.ph.Entity.Spgl;





import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/spgl")
public class SpglServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action=req.getParameter("action");
        SpglDao spglDao=new spglDaoimpl();
        if ("selectAll".equals(action)){
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String goodsStatus = req.getParameter("goodsStatus");
            String goodsName = req.getParameter("goodsName");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            //封装到map
            Map<String,String> map=new HashMap();
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("goodsStatus",goodsStatus);
            map.put("goodsName",goodsName);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("shopName",shopName);
            //连接数据库
            List<Spgl> list = spglDao.selectAll(map);
            //查询总条数
            int total = spglDao.selectCount(map);
            //响应结果
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)) {
            String goodsId = req.getParameter("goodsId");
            String goodsStatus = req.getParameter("goodsStatus");
            SpglDao spglDao1=new spglDaoimpl();
            spglDao1.update(goodsId,goodsStatus);
        }
    }
}
